package com.nf.service;

import com.nf.entity.Card;
import com.nf.entity.Record;
import com.nf.mapper.CardMapper;
import com.nf.mapper.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private  RecordMapper recordMapper;

    @Transactional
    public Card login(String cardId, String password) {
        Card card =cardMapper.queryCardInfoBycardId(cardId);
        if(card==null){
            System.out.println("当前用户有错");
            return null;
        }else{
            if(password.equals(card.getCardPassword())){
                return card;
            }else{
                System.out.println("密码错误");
                return null;
            }
        }
    }

    @Transactional
    public List<Card> getAllCardInfo() {
        return cardMapper.getAllCardInfo();
    }

    @Transactional
    //查询余额
    public Float queryBanlanceBycardId(String cardId) {
        Card card = cardMapper.queryCardInfoBycardId(cardId);
        if (card==null){
            return null;
        }else{
            return card.getCardBalance();
        }
    }

    //存钱
    public boolean saveMoney(Float money, String recordId) {
        Card card = cardMapper.queryCardInfoBycardId(recordId);
        //判断卡号是否存在
        if(card==null){
            return false;
        }
        //判断账号的是否过了有效期
        if((System.currentTimeMillis() - card.getCardDate().getTime() )>0){
            return false;
        }
        //添加1条流水记录
        Record record = new Record();
        //record.setId(1);//此行代码是不需要，因为数据库是自增的【以后还要oracle是怎么玩的】
        System.out.println("插入之前record id："+record.getId());
        record.setRecordId(recordId);
        record.setRecordMoney(money);
        record.setRecordType(1);
        record.setRecordDay(new Date());
        record.setRecordDes("人民币存款");
        recordMapper.addRecord(record);
        System.out.println("插入之后record id："+record.getId());
        //修改余额，自增
        Float money_new = card.getCardBalance()+ money;
        card.setCardBalance(money_new);
        //由面向过程的思维，想着修改余额，升级到面向对象的思维，修改account这个账号对象所有状态（自然就包含了余额这个字段）
        //年轻的我，总是想着，修改一个余额字段，最后，变成修改这个记录中所有的字段，性能不行。
        //牺牲忽略不计的性能，就可以让这个项目更加好扩展
        cardMapper.updateCard(card);
        return true;
    }

    public boolean getMoney(Float money, String recordId) {
        Card card = cardMapper.queryCardInfoBycardId(recordId);
        //判断卡号是否存在
        if(card==null){
            return false;
        }
        //判断卡号是否到期
        if((System.currentTimeMillis() - card.getCardDate().getTime() )>0){
            return false;
        }
        //3.判断余额是否足够
        if (money>card.getCardBalance()){
            return false;
        }
        //4.每次最多只能取2000元
        if(money>2000){
            return false;
        }
        //5.每天只能取5次
        List<Record> recordList = recordMapper.queryGetMoneyRecord(recordId, new java.sql.Date(System.currentTimeMillis()));
        System.out.println("recordList:"+recordList);
        if (recordList.size()>=5){
            return false;
        }
        //6.每天上限是8000元
        Float getMoneyCount = null;
        for (Record r1:recordList){
            getMoneyCount = getMoneyCount + r1.getRecordMoney();
        }
        if(getMoneyCount>=8000){
            return false;
        }
        //算手续费：每次手续费1%,上限是5元
        Float poundage = money/100;
        if (poundage>5){
            poundage =5f;
        }
        //8.插入2条流水记录（本金，手续费）
        Record principalRecord = new Record();
        principalRecord.setRecordId(recordId);
        principalRecord.setRecordMoney(money);
        principalRecord.setRecordType(2);
        principalRecord.setRecordDes("取钱[本金]"+money);
        principalRecord.setRecordDay(new java.util.Date());
        recordMapper.addRecord(principalRecord);

        Record poundageRecord = new Record();
        poundageRecord.setRecordId(recordId);
        poundageRecord.setRecordMoney(poundage);
        poundageRecord.setRecordType(2);
        poundageRecord.setRecordDes("取钱[手续费]"+poundage);
        poundageRecord.setRecordDay(new java.util.Date());
        recordMapper.addRecord(poundageRecord);

        //9.余额减少
        Float balance_new = card.getCardBalance()- money - poundage;
        card.setCardBalance(balance_new);
        cardMapper.updateCard(card);
        return true;
    }
}
